package com.monochromebytes.mononet.test;
import org.newdawn.slick.geom.Shape;

public class Collider {
    public static boolean isColliding(Collidable collidableA, Collidable collidableB) {
        return isColliding(collidableA, collidableB, 0);
    }

    public static boolean isColliding(Collidable collidableA, Collidable collidableB, float delta) {
        Shape shapeA = collidableA.getShape();
        Shape shapeB = collidableB.getShape();

        if (delta != 0) {
            shapeA = collidableA.calculateShape(shapeA, delta);
            shapeB = collidableB.calculateShape(shapeB, delta);
        }

        if (collidableA instanceof World) {
            return Collider.checkCollidingWC(shapeA, shapeB);
        }
        else if (collidableB instanceof World) {
            return Collider.checkCollidingWC(shapeB, shapeA);
        }
        else {
            return Collider.checkCollidingCC(shapeA, shapeB);
        }
    }

    private static boolean checkCollidingWC(Shape worldShape, Shape collidableShape) {
        float[] collidableCenter = collidableShape.getCenter();
        return worldShape.intersects(collidableShape)
            || !worldShape.contains(collidableCenter[0], collidableCenter[1]);
    }

    private static boolean checkCollidingCC(Shape shapeA, Shape shapeB) {
        float[] centerA = shapeA.getCenter();
        float[] centerB = shapeB.getCenter();
        return shapeA.intersects(shapeB) || !shapeA.contains(centerB[0], centerB[1]) ||
            shapeB.intersects(shapeA) || !shapeB.contains(centerA[0], centerA[1]);
    }
}
